import 'package:flutter/material.dart';

class ScaffoldDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {

    return _ScaffoldDemoState();
  }
}

class _ScaffoldDemoState extends State<ScaffoldDemo> with SingleTickerProviderStateMixin {
  int _selectedIndex = 0;
  TabController _tabController;
  List _tabs = ['新闻', '历史', '图片', '视频', '体育', '财经', '我的', '动漫', '战争', '动作', '小说'];

  @override
  void initState() {
    super.initState();

    // 创建Controller
    _tabController = TabController(length: _tabs.length, vsync: this);
  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: Text('导航栏，Tab，抽屉等'),

        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.help),
            onPressed: () {
              Navigator.of(context).pushNamed('/container/scaffold2');
            },
          ),
        ],
        
        bottom: TabBar(
          tabs: _tabs.map((e) => Tab(text: e,)).toList(),
          controller: _tabController,
          isScrollable: true,
        ),
      ),
      
      drawer: MyDrawer(),

      body: TabBarView(
        children: _tabs.map((e) {
          return Center(
            child: Text(e,
              textScaleFactor: 5,
              style: TextStyle(color: Colors.green),
            ),
          );
        }).toList(),
        controller: _tabController,
      ),
      
      bottomNavigationBar: BottomNavigationBar(
        items: [
          BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('首页')),
          BottomNavigationBarItem(icon: Icon(Icons.business), title: Text('商业')),
          BottomNavigationBarItem(icon: Icon(Icons.school), title: Text('学校')),
          BottomNavigationBarItem(icon: Icon(Icons.account_circle), title: Text('我的')),
        ],
        fixedColor: Colors.blue,
        unselectedItemColor: Colors.black54,
        showUnselectedLabels: true,
        currentIndex: _selectedIndex,
        onTap: (index) {
          setState(() {
            _selectedIndex = index;
          });
        },
      ),
      
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Navigator.of(context).maybePop();
        },
        child: Icon(Icons.backup),
      ),
    );
  }
}

class MyDrawer extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    return Drawer(
      child: MediaQuery.removePadding(
          context: context,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              Padding(
                padding: EdgeInsets.only(top: 70),
                child: Row(
                  children: <Widget>[
                    Padding(
                      padding: EdgeInsets.symmetric(horizontal: 16),
                      child: ClipOval(
                        child: Image.asset(
                          'images/icon-40.png',
                          width: 40,
                        ),
                      ),
                    ),
                    Text('头像',
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 16,
                      color: Colors.blue,
                    ),
                    ),
                  ],
                ),
              ),

              Expanded(
                child: ListView(
                  children: <Widget>[
                    ListTile(
                      leading: Icon(Icons.add),
                      title: Text('添加账号'),
                    ),
                    ListTile(
                      leading: Icon(Icons.settings),
                      title: Text('设置'),
                    ),
                  ],
                ),
              ),
            ],
          ),
      ),
    );
  }
}